package cn.schoolwow.quickdao.test.module.business.user.dml.user.flow.delete;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.test.entity.User;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.instanceList.QueryInstanceList;
import org.junit.Assert;

import java.util.List;

public class DeleteUserFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        singleDelete(flowContext);
        multipleDelete(flowContext);
    }

    @Override
    public String name() {
        return "删除用户";
    }

    private void singleDelete(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        User insertUser = (User) flowContext.checkData("insertUser");

        int effect = dao.delete(User.class, insertUser.getId());
        Assert.assertEquals(1, effect);
    }

    private void multipleDelete(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        List<User> insertUserList = (List<User>) flowContext.checkData("insertUserList");

        List<Long> idList = QueryInstanceList.newQuery(insertUserList)
                .execute()
                .getSingleFieldValueList("id");
        int effect = dao.query(User.class)
                .addInQuery("id", idList)
                .execute()
                .delete();
        Assert.assertEquals(insertUserList.size(), effect);
    }
}
